﻿Imports System.IO

Partial Class Reportes_RPTCAPPersonas
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            'cargar proyectos
            'traerProyectos()
        End If


    End Sub
    'Sub traerProyectos() ' carga el control ddlNombreProyecto con los proyectos que han ingresado
    '    Dim u As New Web_Usuario
    '    Using h As New Helper
    '        u = h.GetUsuario(Context.User.Identity.Name)
    '    End Using
    '    Using bd As New BdHogaresDataContext
    '        Dim a = From i In bd.Web_Proyectos
    '                Where (i.web_Operadore.idOperador = u.IdOperador) Or IsNothing(u.IdOperador)
    '                Select i.idProyecto, i.CodigoProyecto
    '        ddlProyectos.DataSource = a
    '        ddlProyectos.DataValueField = "idProyecto"
    '        ddlProyectos.DataTextField = "CodigoProyecto"
    '        ddlProyectos.DataBind()
    '    End Using
    'End Sub

    Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        cargardatos()
    End Sub
    Sub cargardatos()
        Using bd As New bdReportesDataContext
            grdResultado.DataSource = bd.usp_RptCAPPersonas()
            grdResultado.DataBind()
        End Using
    End Sub
    Protected Sub btnExpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnExpt.Click

    End Sub

    Protected Sub exportarAExcel()


        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page

        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)

        'form.Controls.Add(tabla)
        form.Controls.Add(Me.grdResultado)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Reporte.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    Protected Sub btnExp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExp.Click
        cargardatos()
        exportarAExcel()
    End Sub


End Class
